package com.service.admin.product.controller;

import com.service.admin.product.controller.rep.ProductPageDetailRep;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.ProductEnums;
import com.service.admin.framework.log.RequestLog;
import com.service.admin.product.controller.req.ProductPageReq;
import com.service.admin.product.controller.req.ProductAddReq;
import com.service.admin.product.controller.req.UpdateNumReq;
import com.service.admin.product.service.ProductOtherService;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年10月09日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "产品相关接口")
@RestController
@RequestMapping("product-other")
public class ProductOtherController extends BaseController {

    @Autowired
    private ProductOtherService productOtherService;

    @ApiOperation("分页")
    @PostMapping("page")
    public PageResponse<ProductPageDetailRep> page(@ApiParam @RequestBody ProductPageReq pageReq) {
        return successQueryPageResponse(productOtherService.pageProduct(pageReq));
    }

    @RequestLog("产品创建")
    @ApiOperation("添加产品")
    @PostMapping("create")
    public BaseResponse add(@ApiParam @RequestBody @Validated ProductAddReq productAddReq){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", productAddReq, uuid);
        productOtherService.addProduct(productAddReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @RequestLog("产品详情")
    @GetMapping("detail/{id}")
    public OneResponse<ProductPageDetailRep> detail(@PathVariable("id") Integer id) {
        return successQueryOneResponse(productOtherService.detail(id));
    }

    @ApiOperation("添加产品")
    @PostMapping("update/{id}")
    public BaseResponse update(@PathVariable("id") Integer id, @ApiParam @RequestBody @Validated ProductAddReq productAddReq){
        String uuid = UUIDUtil.randomUUID();
        productAddReq.setId(id);
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", productAddReq, uuid);
        productOtherService.modifyProduct(productAddReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("上架产品")
    @PostMapping("shelf/{id}")
    public BaseResponse shelfProduct(@ApiParam @PathVariable("id") Integer id){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        productOtherService.updateProductStatus(id, ProductEnums.Status.PUT_AWAY);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("下架产品")
    @PostMapping("lower/{id}")
    public BaseResponse lowerProduct(@ApiParam @PathVariable("id") Integer id){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        productOtherService.updateProductStatus(id, ProductEnums.Status.TAKE_OFF);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("删除产品")
    @PostMapping("delete/{id}")
    public BaseResponse delete(@ApiParam @PathVariable("id") Integer id){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        productOtherService.updateProductStatus(id, ProductEnums.Status.DELETE);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("修改库存")
    @PostMapping("update/stock/{id}")
    public BaseResponse updateStock(@PathVariable("id") Integer id,@RequestBody @Validated UpdateNumReq numReq) {
        String uuid = UUIDUtil.randomUUID();
        numReq.setId(id);
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", numReq, uuid);
        productOtherService.updateStockNum(numReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("修改排序")
    @PostMapping("update/sort/{id}")
    public BaseResponse updateSort(@PathVariable("id") Integer id,@RequestBody @Validated UpdateNumReq numReq) {
        String uuid = UUIDUtil.randomUUID();
        numReq.setId(id);
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", numReq, uuid);
        productOtherService.updateSort(numReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("修改会员购买数量")
    @PostMapping("update/buy-num/{id}")
    public BaseResponse updateBuyNum(@PathVariable("id") Integer id,@RequestBody @Validated UpdateNumReq numReq) {
        String uuid = UUIDUtil.randomUUID();
        numReq.setId(id);
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        productOtherService.updateBuyNum(numReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

}
